package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("type")
public class TbTypeTemplateController {

    @Reference
    private TbTypeTemplateService tbTypeTemplateService;

    @Reference
    private BrandService brandService;

    @Reference
    private TbSpecificationService tbSpecificationService;

    @RequestMapping("list")
    public String toList(Model model){
        // 查询品牌表
        List<TbBrand> brand = brandService.listBrands();
        model.addAttribute("brand",brand);
        // 查询规格表
        List<TbSpecification> spec = tbSpecificationService.getSpecInfo(null);
        model.addAttribute("spec",spec);
        return "admin/type_template";
    }

    @RequestMapping("getTypeInfo")
    @ResponseBody
    public Result getTypeInfo(String name){
        List<TbTypeTemplate> list = tbTypeTemplateService.getTypeInfo(name);
        return Result.build(ConstantEnum.SUCCESS).setRows(list);
    }

    @RequestMapping("insertTypeInfo")
    @ResponseBody
    public Result insertTypeInfo(TbTypeTemplate tbTypeTemplate){
        if (tbTypeTemplate==null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
            tbTypeTemplateService.insertTypeInfo(tbTypeTemplate);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("updateType")
    @ResponseBody
    public Result updateType(Long id){
        if (id==null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        TbTypeTemplate type = tbTypeTemplateService.updateType(id);
        return Result.build(ConstantEnum.SUCCESS).setRows(type);
    }

    @RequestMapping("updateTypeInfo")
    @ResponseBody
    public Result updateTypeInfo(TbTypeTemplate tbTypeTemplate){
        if (tbTypeTemplate==null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        tbTypeTemplateService.updateTypeInfo(tbTypeTemplate);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("deleteTypeInfo")
    @ResponseBody
    public Result deleteTypeInfo(Long id){
        if (id==null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        tbTypeTemplateService.deleteTypeInfo(id);
        return Result.build(ConstantEnum.SUCCESS);
    }
}
